package com.iotteam.androidbase.moudules.main.view;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.iotteam.androidbase.R;
import com.iotteam.androidbase.base.BaseApplication;
import com.iotteam.androidbase.base.BaseFragment;
import com.iotteam.androidbase.commen.Config;
import com.iotteam.androidbase.commen.LocalInfoManager;
import com.iotteam.androidbase.commen.utils.ToastUtil;
import com.iotteam.androidbase.moudules.login.model.LoginInfo;
import com.iotteam.androidbase.moudules.main.model.CommenInfoMainData;
import com.iotteam.androidbase.moudules.main.presenter.CommenInfoCompl;
import com.iotteam.androidbase.moudules.main.presenter.ICommenInfoPresenter;
import com.iotteam.androidbase.moudules.map.view.MapInfoActivity;
import com.iotteam.androidbase.moudules.sensor_detail.view.SensorInfoActivity;
import com.iotteam.androidbase.moudules.sensor_history.view.SensorHistoryInfoActivity;
import com.iotteam.androidbase.moudules.sensor_setting.view.SensorSettingActivity;
import com.iotteam.androidbase.moudules.test_sensor.TestChartActivity;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by lidechen on 5/2/17.
 */

public class CommenInfoFragment extends BaseFragment implements ICommenInfoView {

    protected View mRootView;
//    private SwipeRefreshLayout mSwipeRefreshLayout;

    private ICommenInfoPresenter mCommenInfoPresenter;
    private GridView mMainGrid;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        if(mRootView == null) {
            mRootView = inflater.inflate(R.layout.fragment_commen, container, false);
        }

//        mSwipeRefreshLayout = (SwipeRefreshLayout) mRootView.findViewById(R.id.swiprefresh);
//        mSwipeRefreshLayout.setRefreshing(false);

        mMainGrid = (GridView) mRootView.findViewById(R.id.gv_mian);

        ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();
        /*for(int i=0;i<10;i++)
        {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("ItemImage", R.mipmap.ic_launcher);//添加图像资源的ID
            map.put("ItemText", "NO."+String.valueOf(i));//按序号做ItemText
            lstImageItem.add(map);
        }*/

        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("ItemImage", R.mipmap.ic_launcher);//添加图像资源的ID
        map.put("ItemText", "实时信息");//按序号做ItemText
        lstImageItem.add(map);

        HashMap<String, Object> mapLocation = new HashMap<String, Object>();
        mapLocation.put("ItemImage", R.mipmap.ic_launcher);//添加图像资源的ID
        mapLocation.put("ItemText", "位置信息");//按序号做ItemText
        lstImageItem.add(mapLocation);

        HashMap<String, Object> mapHistoryInfo = new HashMap<String, Object>();
        mapHistoryInfo.put("ItemImage", R.mipmap.ic_launcher);//添加图像资源的ID
        mapHistoryInfo.put("ItemText", "历史信息");//按序号做ItemText
        lstImageItem.add(mapHistoryInfo);

        HashMap<String, Object> mapSensorSetting = new HashMap<String, Object>();
        mapSensorSetting.put("ItemImage", R.mipmap.ic_launcher);//添加图像资源的ID
        mapSensorSetting.put("ItemText", "设置传感器");//按序号做ItemText
        lstImageItem.add(mapSensorSetting);

        HashMap<String, Object> demoSensorSetting = new HashMap<String, Object>();
        demoSensorSetting.put("ItemImage", R.mipmap.ic_launcher);//
        demoSensorSetting.put("ItemText", "demo");//
        lstImageItem.add(demoSensorSetting);

        SimpleAdapter saImageItems = new SimpleAdapter(BaseApplication.getAppContext(),
                lstImageItem,//数据来源
                R.layout.layout_main_grid_item,//night_item的XML实现
                //动态数组与ImageItem对应的子项
                new String[] {"ItemImage","ItemText"},

                //ImageItem的XML文件里面的一个ImageView,两个TextView ID
                new int[] {R.id.ItemImage,R.id.ItemText});
        //添加并且显示
        mMainGrid.setAdapter(saImageItems);
        //添加消息处理
        mMainGrid.setOnItemClickListener(new ItemClickListener());

//        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
//            @Override
//            public void onRefresh() {
//                Toast.makeText(getActivity(), "onRefresh", Toast.LENGTH_SHORT).show();
//            }
//        });

        mCommenInfoPresenter = new CommenInfoCompl(CommenInfoFragment.this);

        mIsCreateView = true;
        return mRootView;
    }

    @Override
    protected void lazyLoad() {
        mCommenInfoPresenter.loadData();
    }

    @Override
    public void startRefreshView() {
        //mSwipeRefreshLayout.setRefreshing(true);
    }

    @Override
    public void endRefreshView() {
        //mSwipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void updateMainAdapter(CommenInfoMainData data) {

    }

    class  ItemClickListener implements AdapterView.OnItemClickListener {
        public void onItemClick(AdapterView<?> arg0,//The AdapterView where the click happened
                                View arg1,//The view within the AdapterView that was clicked
                                int arg2,//The position of the view in the adapter
                                long arg3//The row id of the item that was clicked
        ) {

            //检查是否登陆
/*            if(!LocalInfoManager.getInstance().isLogin()){
                //未登陆弹出请登陆对话框
                ToastUtil.showShort(R.string.dialog_login_request);
                return;
            }*/

            //在本例中arg2=arg3
            HashMap<String, Object> item = (HashMap<String, Object>) arg0.getItemAtPosition(arg2);
            //显示所选Item的ItemText
            //setTitle((String)item.get("ItemText"));

            Activity activity = getActivity();

            if(arg2 == 0){
                SensorInfoActivity.launch(activity);
            }else if(arg2 == 1){
                MapInfoActivity.launch(activity);
            }else if(arg2 == 2){
                SensorHistoryInfoActivity.launch(activity);
            }else if(arg2 == 3){
                SensorSettingActivity.launch(activity);
            }else if(arg2 == 4){
                TestChartActivity.launch(activity);
            }
        }
    }
}
